package com.aner.tester.resolver;

import com.aner.tester.constant.Constant;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * resolver factory
 *
 * @author aner
 * @version 1.0
 * @date 2020/12/14 22:14
 */
public class ResolverFactory {
    /**
     * get simple type name
     *
     * @param parameterType
     * @return
     */
    public static String getTypeSimpleName(Type parameterType) {
        String typeName = parameterType.getTypeName();
        return getTypeSimpleName(typeName);
    }

    public static String getTypeSimpleName(String typeName) {
        return typeName.substring(typeName.lastIndexOf(".") + 1);
    }

    public static String getArrayTypeName(String genericTypeName) {
        return genericTypeName.substring(genericTypeName.length() - 2);
    }

    /**
     * get generic type name of list
     *
     * @param genericTypeName
     * @return
     */
    public static String[] getCollectionGenericName(String genericTypeName) {
        String name = genericTypeName.substring(genericTypeName.lastIndexOf("<") + 1, genericTypeName.lastIndexOf(">"));

        String[] result = name.split(",");
        return Arrays.stream(result).map(String::trim).collect(Collectors.toList()).toArray(result);
    }

    /**
     * get generic type name of array
     *
     * @param genericTypeName
     * @return
     */
    public static String getArrayGenericName(String genericTypeName) {
        // array
        return genericTypeName.substring(0, genericTypeName.length() - 2);

    }

    public static boolean isArray(String genericTypeName) {
        return genericTypeName.endsWith(Constant.ARRAY);
    }


}
